package capricornus.components.flower
{
	import capricornus.core.ClassManager;
	import capricornus.core.StageManager;
	import capricornus.utils.roughtime.RoughTimer;
	import capricornus.utils.roughtime.RoughTimerEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	internal class FlowerLayer extends Sprite
	{
		private var _num:int = 99;
		private var _numMax:int = 99;
		private var _timer:RoughTimer;
		
		public function FlowerLayer()
		{
			super();
			init();
		}
		
		private function init():void
		{
			this.mouseChildren = false;
			this.mouseEnabled = false;
			
			_timer = new RoughTimer( 300 );
			_timer.addEventListener( RoughTimerEvent.TIMER, onTimer, false, 0, true );
		}
		
		public function play99():void
		{
			_num = 20;
			_numMax = 99;
			_timer.duration = 300;
			_timer.reset();
			_timer.start();
		}
		
		public function play999():void
		{
			_num = 100;
			_numMax = 999;
			_timer.duration = 200;
			_timer.reset();
			_timer.start();
		}
		
		private var _isLeft:Boolean = false;
		private function onTimer( e:RoughTimerEvent ):void
		{
			_isLeft = !_isLeft;
			if( _num > 2 )
			{
				showRightNext();
				num--;
				showLeftNext();
				num--;
			}
			else
			{
				if( numChildren==0 )
				{
					_timer.stop();
					finish();
				}
			}
		}
		
		private function get num():int
		{
			return _num;
		}
		
		private function set num( value:int ):void
		{
			_num = value;
			if( _numMax==999 )
			{
				if( _num==80 || _num==50 || _num==30 )
				{
					showHeart();	
				}
			}
		}
		
		private function finish():void
		{
			dispatchEvent( new Event( "_FINISH" ) );
		}
		
		private static var _heart:Sprite;
		private function showHeart():void
		{
			if( _heart==null ) _heart = ClassManager.getClassInstance( "HeartPosPanel" ) as Sprite;
			var w:int = StageManager.stageWidth;
			var h:int = StageManager.stageHeight;
			
			var x:int = w * 0.5 * Math.random() + w * 0.2;
			var y:int = h * 0.4 * Math.random() + h * 0.2;
			var len:int = _heart.numChildren;
			for( var i:int=0; i<len; i++ )
			{
				var dis:DisplayObject = _heart.getChildAt(i);
				var flower:Flower = new Flower( 3, 0.3 );
				flower.setLocation( x, y );
				flower.fastFlyTo( x + dis.x, y + dis.y );
				addChild( flower.bitmap );
			}
		}
		
		private function showRightNext():void
		{
			var w:int = StageManager.stageWidth;
			var h:int = StageManager.stageHeight;
			
			var x:int = w * 0.8 * Math.random();
			var y:int = -10;
			var flower:Flower = new Flower( Math.random() * 4, Math.random() * 0.6 + 0.1  );
			addChild( flower.bitmap );
			flower.setLocation( x, y );
			flower.flyTo( w * 0.8 * Math.random() + w*0.2, h  );
		}
		
		private function showLeftNext():void
		{
			var w:int = StageManager.stageWidth;
			var h:int = StageManager.stageHeight;
			
			var x:int = w * 0.8 * Math.random() + w * 0.2;
			var y:int = -10;
			var flower:Flower = new Flower( Math.random() * 4, Math.random() * 0.6 + 0.1  );
			addChild( flower.bitmap );
			flower.setLocation( x, y );
			flower.flyTo( w * 0.8 * Math.random(), h  );
		}
	}
}